package com.ui.zhong.webview.webviewproc.webviewclient

import android.graphics.Bitmap
import android.util.Log
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import com.ui.zhong.webview.WebViewCallback

class CustomWebViewClient(val callback: WebViewCallback?) : WebViewClient() {
    private val TAG = "WebViewClient"

    override fun onPageStarted(view: WebView?, url: String, favicon: Bitmap?) {
        super.onPageStarted(view, url, favicon)
        callback?.pageStarted(url) ?: Log.e(TAG, "WebViewCallback is null!!!")
    }

    override fun onPageFinished(view: WebView?, url: String) {
        super.onPageFinished(view, url)
        callback?.pageFinished(url) ?: Log.e(TAG, "WebViewCallback is null!!!")
    }

    override fun onReceivedError(
        view: WebView?,
        request: WebResourceRequest?,
        error: WebResourceError?
    ) {
        super.onReceivedError(view, request, error)
        callback?.onError() ?: Log.e(TAG, "WebViewCallback is null!!!")
    }

//    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
//        val url = request?.url.toString()
//        return try {
//            if (url.startsWith("http:") || url.startsWith("https:")) {
//                view?.loadUrl(url);
//            } else {
//                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
//                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
//                startActivity(BaseApplication.sApplication, intent, null)
//            }
//            true;
//        } catch (e: Exception) {
//            false
//        }
//    }
}